//
//  stats_mcast_server.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// 


#ifndef STATS_MCAST_SERVER_HPP
#define STATS_MCAST_SERVER_HPP
#include <boost/enable_shared_from_this.hpp>
#include <boost/thread.hpp>

#include "components/service/mcast_service_process.hpp"
#include "components/protocol/stats_server_processor.hpp"

class stats_mcast_server:private boost::noncopyable
{

    ba::io_service& ios_;

    boost::shared_ptr<components::service::mcast_service_process> mcast_service_process_;

    bt::seconds persist_interval_;

    components::protocol::stats_server_processor processor_fsm_;
    stats_message_server_processor message_processor_;
    int threads_;
    boost::thread_group thread_group_;
    boost::shared_ptr<ba::io_service::work> service_work_;
    void setup_timer();
public:
    void handle_service_done   (bs::error_code const& ec,const crypto_error& eec);
    stats_mcast_server(ba::io_service& ios, udp_endpoint_t& local_if, udp_endpoint_t& mgroup, int ttl,
                       crypto_buf_t& iv, crypto_buf_t& key,
                       bt::seconds persist_interval,
                       bt::minutes expected_lifetime,
                       std::string const& persist_path,int threads,
                       std::string const& cluster,std::string const& node);
    bool run();
    bool cancel();
    bool shutdown();

};
class shutdown_exception:public std::runtime_error
{
public:
    shutdown_exception(const std::string& arg):std::runtime_error(arg){}
};
#endif // STATS_MCAST_SERVER_HPP
